layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	// 加载页面数据
	var agentData = '';
	$.ajax({
		url : '/admin/agent/show',
		type : 'get',
		dataType : 'json',
		success : function(data){
			agentData = data;
			if(window.sessionStorage.getItem('addAgent')){
				var addAgent = window.sessionStorage.getItem('addAgent');
				agentData = JSON.parse(addAgent).concat(agentData);			
			}			
			agentList();
			for(var i =0; i<agentData.length; i++){
　　             $("#dosm").append("<option value='"+agentData[i].id+"'>" +agentData[i].name+ "</option>")  
                }
      		form.render();
		}
	});

	$(".search_btn").click(function(){
		var newArray = [];
		if($("#dosm").val() != ''){
			var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
			setTimeout(function(){
				$.ajax({
					url : '/admin/agent/show',
					type : 'get',
					data : {'agent_id' : $("#dosm").val()},
					dataType : 'json',
					success : function(data){
						agentData = data;
						if(window.sessionStorage.getItem('addAgent')){
							var addAgent = window.sessionStorage.getItem('addAgent');
							agentData = JSON.parse(addAgent).concat(agentData);			
						}
						agentList();
					}
				})
			})
			layer.close(index);
		}
	})

	function agentList(that){
		
		var nums = 10;
		if(that){
			agentData = that;
		}
		laypage({
			cont : 'page',
			pages : Math.ceil(agentData.length/nums),
			jump : function(obj){
				$(".agent_content").html(renderDate(agentData,obj.curr));
				form.render();
			}
		})

		function renderDate(data,curr){
			var dataHtml = '';
			if(!that){
				currData = agentData.concat().splice(curr*nums-nums, nums);
			}else{
				currData = that.concat().splice(curr*nums-nums, nums);
			}
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					dataHtml += '<tr>'
			    	+'<td>'+((curr-1)*nums+(i+1))+'</td>'
			    	+'<td>'+currData[i].name+'</td>'
			    	+'<td>'
					+  '<a class="layui-btn layui-btn-mini links_edit" agent="'+currData[i].id+'"><i class="iconfont icon-edit"></i> 编辑</a>'
			        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="7">暂无数据</td></tr>';
			}
			return dataHtml;
		}
	}
	//操作
	$("body").on("click",".links_edit",function(){  //编辑
		var _this = $(this);
		id = _this.attr('agent');
        layui.layer.open({
        	title : "编辑代理商名称",
        	type: 2, 
         	content: '/admin/agent/edit?id='+id, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        	area: ['45em', '45em']
        });
	})

	
});


